package com.macrounion.atv.listener;

import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import com.google.common.collect.Lists;
import com.macrounion.atv.cache.ChannelCache;
import com.macrounion.atv.cache.DeviceCache;
import com.macrounion.atv.cache.ProtocolCache;
import com.macrounion.atv.cache.TrapRemoveCache;
import com.macrounion.atv.processor.ChannelProcessor;
import com.macrounion.atv.processor.CurrentDataProcessor;
import com.macrounion.atv.processor.RegProcessor;
import com.macrounion.atv.service.entity.Channel;
import com.macrounion.atv.service.entity.CurrentData;
import com.macrounion.atv.service.entity.ProtocolC;
import com.macrounion.atv.service.entity.ext.DeviceExt;
import com.macrounion.device.listener.ObjectReceiveEvent;
import com.macrounion.device.resolver.dto.SnmpDeviceAttrDetailDto;
import com.macrounion.device.resolver.dto.SnmpDeviceAttrDto;

/**
 * Snmp告警监听
 * @author Administrator
 *
 */
@Component
public class SnmpTrapDeviceDataListener implements com.macrounion.device.listener.ObjectReceiveListener<SnmpDeviceAttrDto>{

	@Autowired
	private DeviceCache deviceCache;
	@Autowired
	private ProtocolCache protocolCache;
	@Autowired
	private ChannelCache channelCache;
	@Autowired
	private ChannelProcessor channelProcessor;
	@Autowired
	private CurrentDataProcessor currentDataProcessor;
	@Autowired
	private TrapRemoveCache trapRemoveCache;
	@Autowired
	private RegProcessor regProcessor;
	
	@Override
	public void receive(ObjectReceiveEvent<SnmpDeviceAttrDto> event) {
		if(!regProcessor.canUse())
			return;
		SnmpDeviceAttrDto dto = event.getObject();
//		System.out.println("接收到告警信息：---------》"+dto.getDeviceIp());
		if(dto == null || StringUtils.isEmpty(dto.getDeviceIp()) || CollectionUtils.isEmpty(dto.getDetails()))
			return;
		DeviceExt device = deviceCache.getDevice(dto.getDeviceIp().split("\\/")[0]);
		if(device == null || !device.getStype().toLowerCase().equals("snmp"))
			return;
		List<ProtocolC> pcs = protocolCache.getByProtocolId(device.getProtocolId());
		if(CollectionUtils.isEmpty(pcs))
			return;
		List<ProtocolC> trapProtocolc = pcs.parallelStream().filter(p->{return p.getTrap().equals(1);}).collect(Collectors.toList());
		if(CollectionUtils.isEmpty(trapProtocolc))
			return;
		trapSave(dto.getDetails(),trapProtocolc,device);
		trapRemoveCache.saveRemoved(false);//恢复开关打开
	}

	/**
	 * 保存告警数据
	 * @param details
	 * @param trapProtocolc
	 * @param device
	 */
	private void trapSave(List<SnmpDeviceAttrDetailDto> details, List<ProtocolC> trapProtocolc, DeviceExt device) {
		List<CurrentData> datas = Lists.newArrayList();
		details.forEach(d->{
			ProtocolC pc = trapProtocolc.parallelStream().filter(p->{return p.getMid().equals(d.getOid());}).findFirst().orElse(null);
			if(pc != null) {
				CurrentData data = new CurrentData();
				data.setChannelCode(pc.getChannelCode());
				Channel c = channelCache.getChannel(device.getId(), pc.getChannelCode());
				if(c!=null) {
					data.setId(UUID.randomUUID().toString());
					data.setChannelId(c.getId());
					data.setCreatedTime(new Date());
					data.setDeviceCode(device.getDeviceCode());
					data.setDeviceId(device.getId());
					data.setShowValue(channelProcessor.translateValue(StringUtils.isEmpty(c.getTranslate())?pc.getTranslate():c.getTranslate(), d.getValue()));
					data.setSpotId(device.getSpotId());
					data.setUnit(c.getUnit());
					data.setValues(d.getValue());
					data.setAlarm(1);
					if(device.getSendtrap().equals(1))
						channelProcessor.sendTarpToItu(device,c,data);
					datas.add(data);
				}
			}
		});
		if(CollectionUtils.isEmpty(datas))
			return;
		currentDataProcessor.saveDataFromSnmp(device.getId(),datas,"trap");
	}

}
